package com.fan.shop.services;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;
import javax.inject.Named;

import org.springframework.transaction.annotation.Transactional;

import com.fan.shop.dao.CategoryDao;
import com.fan.shop.domain.Category;

@Named("categoryService")
@Transactional(readOnly = true)
public class CategoryServiceImpl implements CategoryService {

	@Inject
	@Named("categoryDao")
	private CategoryDao categoryDao;

	public CategoryServiceImpl() {
		// TODO Auto-generated constructor stub
	}

	public CategoryDao getCategoryDao() {
		return categoryDao;
	}

	public void setCategoryDao(CategoryDao categoryDao) {
		this.categoryDao = categoryDao;
	}

	public Category getCategory(String name) {
		return categoryDao.getCategory(name);
	}

	public List<Category> getAllCategories() {
		return categoryDao.getAllCategories();
	}

	public int getSize() {
		return categoryDao.getSize();
	}

	@Transactional(readOnly = false)
	public void addCategory(Category category) {
		categoryDao.addCategory(category);

	}

	@Transactional(readOnly = false)
	public void updateCategory(Category category) {
		categoryDao.updateCategory(category);
	}

	@Transactional(readOnly = false)
	public void deleteCategory(Category category) {
		categoryDao.deleteCategory(category);

	}

	public boolean hasCategory(String name) {
		return categoryDao.hasCategory(name);
	}

	public boolean isParent(Category category) {
		if (hasCategory(category.getName())) {
			if (category.getParent() == null) {
				return true;
			}
			for (Category catg : getAllCategories()) {
				if (catg.getParent() == null) {
					continue;
				}
				if (catg.getParent().intValue() == category.getId().intValue()) {
					return true;
				}
			}
			return false;

		} else {
			return false;
		}
	}

	public boolean isRoot(Category category) {
		if (hasCategory(category.getName()) && category.getParent() == null) {
			return true;
		} else {
			return false;
		}
	}

	public List<Category> getSubCategories(Category category) {
		if (!hasCategory(category.getName())) {
			return null;
		} else {
			List<Category> subList = new ArrayList<Category>();
			for (Category cat : getAllCategories()) {
				if (cat.getParent() == null) {
					continue;
				}
				if (cat.getParent().intValue() == category.getId().intValue()) {
					subList.add(cat);
				}
			}
			return subList;
		}

	}

	public boolean isLeaf(Category category) {
		if (hasCategory(category.getName())) {
			if (getSubCategories(category).size() == 0) {
				return true;
			} else {
				return false;
			}
		} else {
			return false;
		}

	}

	public List<Category> getRootCategories() {
		return categoryDao.getRootCategories();
	}

	public boolean isLastLevelFolder(Category category) {

		return false;
	}

	public Category getCategory(Integer id) {
		return categoryDao.getCategory(id);
	}

	public String getCategoryNameByID(Integer id) {
		if (id == null || id == 0) {
			return "";
		}

		return getCategory(id).getName();
	}

	@SuppressWarnings("rawtypes")
	public List getList() {
		return getAllCategories();
	}

	public List<String> getColumnNames() {

		return categoryDao.getColumnNames();
	}

	public int getColumnsSize() {
		return getColumnNames().size();
	}

	public Object copy(Object obj) {
		// TODO Auto-generated method stub
		return null;
	}

	public Category getCategoryByName(String name) {

		return categoryDao.getCategoryByName(name);
	}

}
